<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf">开票列表</div>
                </div>
                <div class="widget-body am-fr">

                    <!-- 工具栏 -->
                    <div class="page_toolbar am-margin-bottom-xs am-cf">
    <form class="toolbar-form am-form-inline" action=""> <!-- 加 am-form-inline -->
        <input type="hidden" name="s" value="/<?= \request()->pathinfo() ?>">

        <!-- 导出按钮放最左 -->
        <div class="am-form-group am-fl am-margin-right-sm">
            <button id="btn-export" type="button" class="am-btn am-btn-secondary">
                <i class="am-icon-download"></i> 导出
            </button>
        </div>

        <div class="am-form-group am-fl am-margin-right-sm">
            <input type="text" class="am-form-field" name="start_time"
                   placeholder="开始日期"
                   value="<?= $request->get('start_time') ?>"
                   data-am-datepicker readonly />
        </div>

        <div class="am-form-group am-fl am-margin-right-sm">
            <input type="text" class="am-form-field" name="end_time"
                   placeholder="结束日期"
                   value="<?= $request->get('end_time') ?>"
                   data-am-datepicker readonly />
        </div>

        <div class="am-form-group am-fl am-margin-right-sm">
            <select name="status" data-am-selected="{btnSize: 'sm', placeholder: '状态'}">
                <option value="">状态</option>
                <option value="0" <?= $request->get('status')==='0' ? 'selected' : '' ?>>待处理</option>
                <option value="1" <?= $request->get('status')==='1' ? 'selected' : '' ?>>已开票</option>
                <option value="2" <?= $request->get('status')==='2' ? 'selected' : '' ?>>已邮寄/已发送</option>
                <option value="3" <?= $request->get('status')==='3' ? 'selected' : '' ?>>驳回</option>
            </select>
        </div>

        <div class="am-form-group am-fl">
            <div class="am-input-group am-input-group-sm tpl-form-border-form">
                <input type="text" class="am-form-field" name="keyword"
                       placeholder="订单ID/订单号/姓名/手机号/邮箱/税号/抬头/备注"
                       value="<?= $request->get('keyword') ?>">
                <div class="am-input-group-btn">
                    <button class="am-btn am-btn-default am-icon-search" type="submit"></button>
                </div>
            </div>
        </div>
    </form>
</div>

                    <div class="am-scrollable-horizontal am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black am-text-nowrap">
                            <thead>
                                <tr>
                                    <th>开票ID</th>
                                    <th>用户姓名</th>
                                    <th>电话</th>
                                    <th>订单ID</th>
                                    <th>订单号</th>
                                    <th>抬头类型</th>
                                    <th>发票抬头</th>
                                    <th>税号</th>
                                    <th>开票内容</th>
                                    <th>金额</th>
                                    <th>方式</th>
                                    <th>收件人/邮箱</th>
                                    <th>备注</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($list as $item): ?>
                                <tr>
                                    <td class="am-text-middle"><?= $item['invoice_id'] ?></td>
                                    <td class="am-text-middle"><?= $item['patient_name'] ?: '—' ?></td>
                                    <td class="am-text-middle"><?= $item['patient_phone'] ?: '—' ?></td>

                                    <td class="am-text-middle"><?= $item['order_id'] ?: '—' ?></td>
                                    <td class="am-text-middle"><?= $item['order_no'] ?: '—' ?></td>

                                    <td class="am-text-middle"><?= $item['title_type_text'] ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['invoice_title']) ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['tax_number']) ?></td>
                                    <td class="am-text-middle"><?= htmlentities($item['invoice_content']) ?></td>
                                    <td class="am-text-middle"><?= $item['amount'] ?></td>
                                    <td class="am-text-middle"><?= $item['invoice_type_text'] ?></td>

                                    <td class="am-text-middle">
                                        <?php if ($item['invoice_type'] == '1'): // 邮寄 ?>
                                            <div><?= htmlentities($item['consignee_name'] ?: '—') ?></div>
                                            <div><?= htmlentities($item['consignee_mobile'] ?: '—') ?></div>
                                            <div class="am-text-truncate" style="max-width:240px;">
                                                <?= htmlentities($item['consignee_address'] ?: '—') ?>
                                            </div>
                                        <?php else: // 邮箱 ?>
                                            <div><?= htmlentities($item['email'] ?: '—') ?></div>
                                        <?php endif; ?>
                                    </td>
                                    <td class="am-text-middle"><?= $item['remark'] ?></td>
                                                                        <td class="am-text-middle"><?= $item['status_text'] ?></td>
                                                                        <td class="am-text-middle"><?= $item['create_time'] ?></td>
                                                                        <td class="am-text-middle">
                                        <a href="javascript:;" 
                                        class="am-btn am-btn-primary am-btn-xs btn-status"
                                        data-id="<?= $item['invoice_id'] ?>"
                                        data-status="<?= (int)$item['status'] ?>"
                                        data-remark="<?= htmlentities($item['remark']) ?>">
                                        处理
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php if ($list->isEmpty()): ?>
                                <tr><td colspan="16" class="am-text-center">暂无记录</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr"><?= $list->render() ?></div>
                        <div class="am-fr pagination-total am-margin-right">
                            <div class="am-vertical-align-middle">总记录：<?= $list->total() ?></div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<!-- 开票状态弹窗 -->
<div class="am-modal am-modal-confirm" tabindex="-1" id="modal-status">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">更改开票状态</div>
    <div class="am-modal-bd">
      <form id="form-status" class="am-form am-form-horizontal">
        <input type="hidden" name="invoice_id" id="invoice_id" value="">

        <div class="am-form-group">
          <label class="am-u-sm-3 am-form-label">状态</label>
          <div class="am-u-sm-9">
            <select name="status" id="status" class="am-form-field">
              <option value="0">待处理</option>
              <option value="1">已开票</option>
              <option value="2">已邮寄/已发送</option>
              <option value="3">驳回</option>
            </select>
          </div>
        </div>

        <div class="am-form-group">
          <label class="am-u-sm-3 am-form-label">备注</label>
          <div class="am-u-sm-9">
            <textarea name="remark" id="remark" rows="3" placeholder="可填写快递单号/邮箱发送说明/驳回原因等"></textarea>
          </div>
        </div>
      </form>
    </div>
    <div class="am-modal-footer">
      <span class="am-modal-btn" data-am-modal-cancel>取消</span>
      <span class="am-modal-btn" id="btn-submit-status">确定</span>
    </div>
  </div>
</div>
<script>
/* 如需导出功能，解除注释并确认路由
$(function () {
    $('#btn-export').on('click', function () {
        var params = $('.toolbar-form').serialize();
        // 注意这里用 request()->pathinfo() 那一套路由风格，使用 url() 生成你的导出地址
        // window.location = "<?= url('invoice/export') ?>" + (params ? ('&' + params) : '');
    });
});
*/
$(function () {
  // 打开弹窗并注入数据
  $(document).on('click', '.btn-status', function () {
    var id = $(this).data('id');
    var status = $(this).data('status');
    var remark = $(this).data('remark') || '';

    $('#invoice_id').val(id);
    $('#status').val(status);
    $('#remark').val(remark);

    $('#modal-status').modal({
      relatedTarget: this,
      closeViaDimmer: false
    });
  });

  // 提交变更
  $('#btn-submit-status').on('click', function () {
    var postData = $('#form-status').serialize();

    $.post("<?= url('neworder.invoice/setStatus') ?>", postData, function (res) {
      if (res.code === 1) {
        $('#modal-status').modal('close');
        // 成功后刷新当前页，保留查询参数
        var loc = window.location;
        window.location = loc.pathname + loc.search;
      } else {
        layer.msg(res.msg || '更新失败', { time: 1500 });
      }
    }, 'json').fail(function () {
      layer.msg('网络错误，请重试', { time: 1500 });
    });
  });
});
$(function () {
  // 导出当前筛选结果
  $('#btn-export').on('click', function () {
    // 生成导出地址（TP5 会返回 index.php?s=/store/neworder.invoice/export 这种）
    var base = "<?= url('neworder.invoice/export') ?>";
    // 采集当前筛选，但排除隐藏的 s，避免重复
    var arr = $('.toolbar-form').serializeArray();
    var pairs = [];
    $.each(arr, function (_, kv) {
      if (kv.name !== 's' && kv.value !== '') {
        pairs.push(encodeURIComponent(kv.name) + '=' + encodeURIComponent(kv.value));
      }
    });
    var qs = pairs.join('&');
    window.location = base + (qs ? ('&' + qs) : '');
  });
});
</script>